package StudyCode.summary.Basic.console;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class Redirecting {
	public static void main(String[] args) {
		try {//from  w  w  w  .  j av a  2 s. co  m
			// Tee standard output
			PrintStream out = new PrintStream(new FileOutputStream("out.log"));
			PrintStream tee = new TeeStream(System.out, out);

			System.setOut(tee);

			// Tee standard error
			PrintStream err = new PrintStream(new FileOutputStream("err.log"));
			tee = new TeeStream(System.err, err);

			System.setErr(tee);
		} catch (FileNotFoundException e) {
		}

		// Write to standard output and error and the log files
		System.out.println("welcome");
		System.err.println("error");
	}
}

class TeeStream extends PrintStream {
	PrintStream out;

	public TeeStream(PrintStream out1, PrintStream out2) {
		super(out1);
		this.out = out2;
	}

	public void write(byte buf[], int off, int len) {
		try {
			super.write(buf, off, len);
			out.write(buf, off, len);
		} catch (Exception e) {
		}
	}

	public void flush() {
		super.flush();
		out.flush();
	}
}
